var updatesellinformation = new Vue({
    el:'#updatesellinformation',
    //初始化数据类型
    data:{
    //获取到的验证码信息
    securityCode: '',
    //用户填写的验证码
    securityCodeUser: '',
    options: [],
    optionsUser:[],
    goodsType:{},
    Merchant:{},
    map:{},
    importFileUrl: '/home/merchant/updateSell',
    },
    //时间提交方法
    methods:{
        //获取手机验证码
        telCode:function(){
            var _this = this;
            $("#input5").attr("disabled","disabled");
            $.post("/home/warnInfo/sendInfo",{"tel":_this.Merchant.tel},function(data){
                if (data != "500") {
                    _this.securityCode = data;
                    _this.$message.success("验证码已发送!");
                    sendMessage();
                } else {
                    _this.$message.error("获取验证码失败！");
                }
            });
        },
        sellsenter:function(){
            var _this = this;
            debugger
            _this.Merchant.goodsType =_this.goodsType;
            _this.Merchant.affiliatedRegional = $("#province1").val()+"-"+$("#city1").val()+"-"+$("#district1").val();
            _this.map.Merchant =JSON.stringify(_this.Merchant);
            _this.$refs.upload.submit();
            $("#submit").attr("disabled","disabled")
        },
        //文件上传成功后的回调函数
        handleAvatarSuccess: function (response, file, fileList) {
            if (response == "商家信息修改成功!") {
                this.$message.success(response);
            } else {
                this.$message.error("商家信息修改失败!");
                $("#submit").removeAttr("disabled");
            }

        },
        //文件上传失败的回调函数
        handleAvatarError: function (err, file, fileList) {
            this.$message.error("失败！" + err);
            $("#submit").removeAttr("disabled");
        },
    },
    //页面加载完成后执行此方法
    created:function(){
        var _this = this;
        $.post("/home/menu/select", "", function (data) {
            for (var i = 0; i < data.data.Menu.length; i++) {
                var map1 = {};
                map1.children = [];
                map1.value = data.data.Menu[i].id;
                map1.label = data.data.Menu[i].name;
                _this.options.push(map1);
            }
        });

        $.post("/sys/user/seleceOfficeUser", "", function (data) {
            for (var i = 0; i < data.data.sysUser.length; i++) {
                var map1 = {};
                map1.value = data.data.sysUser[i].id;
                map1.label = data.data.sysUser[i].name;
                _this.optionsUser.push(map1);
            }
        });

    }
});
//file表单数据赋予input[type="text"]
$(".el-upload__input").change(function(){
    var see =  $(".el-upload__input").val()
    $("#uploadFile").val(see);
});
//页面加载完成执行此程序
$(function(){
    $("#code").attr("disabled","disabled");
    v_companyname();v_telphone();v_email();v_idCardNo();v_xmname();
    v_institutionCode();v_businessModel(); v_affiliatedRegional();
    v_code();
});
//提交按钮状态设置
function enableSubmit(bool){
    if(bool) {
        $("#submit").removeAttr("disabled");
    } else $("#submit").attr("disabled","disabled");
}

//验证多少项，多少个false
var flags = [false,false,false,false,false,false,false,false,false,false];
//flags全为true时提交按钮解除禁用
function v_submitbutton(){
    for(f in flags) if(!flags[f]) {
        enableSubmit(false);
        return;
    }
    enableSubmit(true);
}
//帐号验证
var RegUserName = /^[a-zA-Z0-9_-]{4,16}$/;
//邮箱验证的正则
var RegEmail = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
//手机号码验证
var RegTel = /^1[0-9]\d{9}$/;
//身份证号验证
var RegidCardNo = /^[0-9]{17}(\d|[xX])|[0-9]{14}(\d|[xX])$/;
//组织机构代码验证
var ReginstitutionCode = /^[0-9]{9}$/;
//图片验证
var RegComPanyName = /^\S+\.(jpg|png|)$/;

/*//验证帐号
function v_username(){
    var username = $("#input1").val();
    if(!RegUserName.test(username)) { flags[0]=false;enableSubmit(false);
        $("#tab1").html(" &nbsp;帐号格式错误");$("#img-icon1-2").css("display","block");$("#input1").css("border","solid 1px red");}
    else{flags[0] = true;
        $("#input1").css("border","solid 1px #2dc12d","");$("#img-icon1-1").css("display","block");$("#tab1").css("display","none");$("#img-icon1-2").css("display","none");}
    v_submitbutton();
}*/
//验证公司名称
function v_companyname(){
    var companyname = $("#input4").val();
    if(companyname.trim()=="") { flags[0]=false;enableSubmit(false);
        $("#tab2").html("此项不能为空");$("#img-icon2-2").css("display","block");$("#input4").css("border","solid 1px red");}
    else{flags[0] = true;
        $("#input4").css("border","solid 1px #2dc12d","");$("#img-icon2-1").css("display","block");$("#tab2").css("display","none");$("#img-icon2-2").css("display","none");}
    v_submitbutton();
}
//验证姓名
function v_xmname(){
    var companyname = $("#input14").val();
    if(companyname.trim()=="") { flags[9]=false;enableSubmit(false);
        $("#tab14").html("此项不能为空");$("#img-icon14-2").css("display","block");$("#input14").css("border","solid 1px red");}
    else{flags[9] = true;
        $("#input14").css("border","solid 1px #2dc12d","");$("#img-icon14-1").css("display","block");$("#tab14").css("display","none");$("#img-icon14-2").css("display","none");}
    v_submitbutton();
}
//验证电话
function v_telphone(){
    var telphone = $("#input5").val();
    if(!RegTel.test(telphone)) { flags[1]=false;enableSubmit(false);
        $("#tab3").html("手机号为11位");
        $("#img-icon3-2").css("display","block");
        $("#input5").css("border","solid 1px red");
        $("#code").attr("disabled","disabled");
        $("#input6").val('');
    } else{flags[1] = true;
        $("#input5").css("border","solid 1px #2dc12d","");
        $("#img-icon3-1").css("display","block");
        $("#tab3").css("display","none");
        $("#img-icon3-2").css("display","none");
        $("#input6").val('');
        $("#code").removeAttr("disabled");

    }
    v_submitbutton();
}
//验证邮箱
function v_email(){
    var email = $("#input7").val();
    if(!RegEmail.test(email)) {flags[2]=false;enableSubmit(false);
        $("#tab5").html("邮箱格式不正确");$("#img-icon5-2").css("display","block");$("#input7").css("border","solid 1px red");}
    else{flags[2] = true;
        $("#input7").css("border","solid 1px #2dc12d","");$("#img-icon5-1").css("display","block");$("#tab5").css("display","none");$("#img-icon5-2").css("display","none");}
    v_submitbutton();
}
//验证身份证号
function v_idCardNo(){
    var idCardNo = $("#input8").val();
    if(!RegidCardNo.test(idCardNo)) { flags[3]=false;enableSubmit(false);
        $("#tab6").html("身份证号错误");$("#img-icon6-2").css("display","block");$("#input8").css("border","solid 1px red");}
    else{flags[3] = true;
        $("#input8").css("border","solid 1px #2dc12d","");$("#img-icon6-1").css("display","block");$("#tab6").css("display","none");$("#img-icon6-2").css("display","none");}
    v_submitbutton();
}
//验证组织机构代码
function v_institutionCode(){
    var institutionCode = $("#input9").val();
    if(institutionCode.trim()=="") { flags[4]=false;enableSubmit(false);
        $("#tab7").html("输入错误");$("#img-icon7-2").css("display","block");$("#input9").css("border","solid 1px red");}
    else{flags[4] = true;
        $("#input9").css("border","solid 1px #2dc12d","");$("#img-icon7-1").css("display","block");$("#tab7").css("display","none");$("#img-icon7-2").css("display","none");}
    v_submitbutton();
}
//验证经营模式
function v_businessModel(){
    var businessModel = $("#input10").val();
    if(businessModel.trim()=="") { flags[5]=false;enableSubmit(false);
        $("#tab8").html("必输项");$("#img-icon8-2").css("display","block");$("#input10").css("border","solid 1px red");}
    else{flags[5] = true;
        $("#input10").css("border","solid 1px #2dc12d","");$("#img-icon8-1").css("display","block");$("#tab8").css("display","none");$("#img-icon8-2").css("display","none");}
    v_submitbutton();
}
//验证省、市
function v_affiliatedRegional(){
    v_goodsType();
    var selected = document.getElementsByClassName("form-control select");
    for(var i=0;i<selected.length;i++){
        if(selected[i].value==""){
            flags[6]=false;enableSubmit(false);
            $("#tab9").html("必输项");$("#province1,#city1,#district1").css("border","solid 1px red");
        }else{
            flags[6] = true;
            $("#province1,#city1,#district1").css("border","solid 1px #2dc12d","");$("#tab9").css("display","none");
        }
        v_submitbutton();
    }
}
//验证商品类型
$(".el-input__inner").on("click", v_goodsType);

function v_goodsType() {
    if (($(".el-input__inner").attr("placeholder")) == "请选择") {
        flags[7] = false;
        enableSubmit(false);
        $("#tab12").html("必输项");
        $("#img-icon10-2").css("display", "block");
        $("#tab12").css("display", "block");
        $(".el-input__inner").css("border", "solid 1px red");
    }
    else if (($(".el-input__inner").attr("placeholder")) != "请选择") {
        flags[7] = true;
        $(".el-input__inner").css("border", "solid 1px #2dc12d", "");
        $("#img-icon10-1").css("display", "block");
        $("#tab12").css("display", "none");
        $("#img-icon10-2").css("display", "none");
    }
    v_submitbutton();
}
//验证验证码

function v_code() {
    var securityCodeUser = $("#input6").val();
    if (securityCodeUser != updatesellinformation.securityCode ||securityCodeUser.trim() =="") {
        flags[8] = false;
        enableSubmit(false);
        $("#tab4").html("请输入4位验证码");
        $("#img-icon4-2").css("display", "block");
        $("#tab4").css("display", "block");
        $("#input6").css("border", "solid 1px red");
        $("#input6").removeAttr("disabled");

    }
    else {
        flags[8] = true;
        $("#input6").css("border", "solid 1px #2dc12d", "");
        $("#img-icon4-1").css("display", "block");
        $("#tab4").css("display", "none");
        $("#img-icon4-2").css("display", "none");
        $("#input5").attr("disabled","disabled");
        $("#input6").attr("disabled","disabled");
        $("#code").attr("disabled","disabled");
        window.clearInterval(InterValObj);
    }
    v_submitbutton();
}

var InterValObj; //timer变量，控制时间
var count = 120; //间隔函数，1秒执行
var curCount;//当前剩余秒数

function sendMessage() {
    curCount = count;
    //设置button效果，开始计时
    $("#code").attr("disabled", "true");
    $("#code").val(curCount+"秒");
    InterValObj = window.setInterval(SetRemainTime, 1000); //启动计时器，1秒执行一次

    //请求后台发送验证码 TODO

}
//timer处理函数
function SetRemainTime() {
    if (curCount == 0) {
        window.clearInterval(InterValObj);//停止计时器
        if(!flags[8]){
            $("#input5").removeAttr("disabled");
            $("#code").removeAttr("disabled");
        }
        $("#code").val("获取验证码");
    }
    else {
        curCount--;
        $("#code").val(curCount+"秒");
    }
}

function adaptValue(){
    return true;
}
